package t20220411;

import java.util.Random;

/**
 * @Author: DengJia
 * @Date: 2022/4/11 21:04
 * @Description:
 */

class Initable {
    static final int STATIC_FINAL = 47;
    static final int STATIC_FINAL2 = ClassInitialization.rand.nextInt(1000);
    static {
        System.out.println("初始化Initable");
    }
}

class Initable2 {
    static int staticNonFinal = 147;
    static {
        System.out.println("初始化Initable2");
    }
}

class Initable3 {
    static int staticNonFinal = 74;
    static{
        System.out.println("初始化Initable3");
    }
}

public class ClassInitialization {
    public static Random rand = new Random(47);

    public static void main(String[] args) throws ClassNotFoundException {
        Class<Initable> initable = Initable.class;
        System.out.println("创建Initable引用之后");

        //不触发初始化
        System.out.println(Initable.STATIC_FINAL);
        //触发初始化
        System.out.println(Initable.STATIC_FINAL2);

        //触发初始化
        System.out.println(Initable2.staticNonFinal);
        Class<?> initable3 = Class.forName("t20220411.Initable3");
        System.out.println("创建Initable3引用之后");
        System.out.println(Initable3.staticNonFinal);

    }
}
